package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void insert(Employee employee) {

    }

    @Override
    public Employee get(Long id) {
        return null;
    }

    @Override
    public List<Employee> listAll() {
        return null;
    }

    @Override
    public void update(Employee employee) {

    }

    @Override
    public IPage<Employee> query(QueryObject qo) {
        Page<Employee> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<Employee> wrapper = Wrappers.<Employee>query();
        return super.page(page,wrapper);
    }

    @Override
    public Employee selectByUername(String username) {
        return employeeMapper.selectByUername(username);
    }

    @Override
    public List<Employee> selectedAll() {
        return employeeMapper.selectList(null);
    }

    //登录时找用户
    @Override
    public Employee login(String username, String password) {
        if (!StringUtils.hasText(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)) {
            throw new RuntimeException("密码不能为空");
        }
        Employee employee = employeeMapper.login(username, password);
        if (employee == null) {
            throw new RuntimeException("用户名和密码错误");
        }
        return employee;
    }

    @Override
    public List<String> selectByEidPermission(Long id) {
        return employeeMapper.selectByEidPermission(id);
    }
}
